package de.gameduell.framework.application.controller {
	import de.gameduell.framework.application.controller.Controller;
	import de.gameduell.framework.application.protocol.MultiplayerProtocol;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.util.FlashVars;
	import de.gameduell.net.src.communication.ICommunication;
	import de.gameduell.net.src.communication.ServerMessageEvent;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;	

	public class MultiplayerController extends Controller implements IMultiplayerController {

		protected var _userName:String;
		protected var _userAvatar:String;
		protected var _players:Array = new Array();
		
		private var _chatEnabled:Boolean;
		private var _chatExists:Boolean;
		private var _chatActive:Boolean;

		override public function setCommunication(aCommunication:ICommunication):void{
			super.setCommunication(aCommunication);
			userName = FlashVars.getKeyValue("user");
			Debug.logHead = userName;
			_chatExists   = false;
			_chatEnabled  = true;
			_chatActive   = true;
					
			communication.addEventListener(MultiplayerProtocol.GO, receiveGo);
			communication.addEventListener(MultiplayerProtocol.AVATAR, receiveAvatar);
			communication.addEventListener(MultiplayerProtocol.PLAYER_LEFT, receivePlayerLeft);
			communication.addEventListener(MultiplayerProtocol.END, receiveEnd);
			communication.addEventListener(MultiplayerProtocol.CHAT, receiveChat);
		}

		public function sendChatMessage(aMessage:String):void {
			var message:String = escape(aMessage);
			if(!_abortedByUser){
				communication.send(MultiplayerProtocol.CHAT, [message]);
			}
		}
		
		public function get players():Array {
			return _players;
		}

		public function get userName():String{
			return _userName;
		}

		public function get chatEnabled():Boolean{
			return _chatEnabled && _chatExists;
		}
		
		public function get chatActive():Boolean{
			return _chatActive;
		}
		
		public function set userName(aName:String):void{
			_userName = aName;
		}
		
		public function set chatEnabled(isEnabled:Boolean):void{
			_chatEnabled = isEnabled;
		}
		
		public function set chatExists(exists:Boolean):void{
			_chatExists = exists;
		}
		
		public function set chatActive(aChatActive:Boolean):void{
			 _chatActive = aChatActive;
		}

		public function itsMyId(id:int):Boolean{
			return _slotid == id;
		}
		
		public function itsMyName(name:String):Boolean{
			return name == FlashVars.getKeyValue("user");
		}
				
		public function sendTimeout():void {
			if(!_abortedByUser){
				communication.send(MultiplayerProtocol.TIMEOUT, [_slotid]);
			}
		}
		
		protected function receiveChat(event:ServerMessageEvent):void {
			if(!_abortedByUser){
				var parameters:Array   = new Array();
				parameters["playerId"] = int(event.parameters[0]);
				parameters["message"]  = unescape(String(event.parameters[1]));
				dispatchEvent(new ServerMessageEvent(event.type, parameters));
			}
		}

		protected function receiveEnd(event:ServerMessageEvent):void {
			if(!_abortedByUser){
				dispatchEvent(new ServerMessageEvent(event.type,event.parameters));
			}
		}

		protected function receivePlayerLeft(event:ServerMessageEvent):void {
			if(!_abortedByUser){
				var parameters:Array = new Array();
				parameters["playerId"] = int(event.parameters[0]);
				if(slotid == parameters["playerId"]){
					communication.terminate();
				}
				dispatchEvent(new ServerMessageEvent(event.type, parameters));
			}
		}

		protected function receiveAvatar(event:ServerMessageEvent):void {
			dispatchEvent(new ServerMessageEvent(event.type,event.parameters));
		}
		
		protected function receiveGo(event:ServerMessageEvent):void {
			dispatchEvent(new ServerMessageEvent(event.type,event.parameters));
		}
		
		override public function exit(event:Event = null):void {
			var winDistribution:String = FlashVars.getKeyValue("wd") == FlashVars.NO_VALUE ? "0" : FlashVars.getKeyValue("wd");
			try {
				navigateToURL(new URLRequest(FlashVars.getKeyValue("durl") + "&wd=" + winDistribution), "_self");
			}
            catch (e:Error) {
				Debug.trace("View::leaveGame: unable to reidirect -> error: " + e, Debug.ERROR);
			}
		}
	}
}
